home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / answers / rec / puzzles / archive / logic / part1 < prev    next >
Text File  |  1993-08-17  |  52KB  |  1,415 lines

  1. Newsgroups: rec.puzzles,news.answers,rec.answers
  2. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!gatech!europa.eng.gtefsd.com!uunet!questrel!chris
  3. From: chris@questrel.com (Chris Cole)
  4. Subject: rec.puzzles Archive (logic), part 22 of 35
  5. Message-ID: <puzzles/archive/logic/part1_745653851@questrel.com>
  6. Followup-To: rec.puzzles
  7. Summary: This is part of an archive of questions
  8.  and answers that may be of interest to
  9.  puzzle enthusiasts.
  10.  Part 1 contains the index to the archive.
  11.  Read the rec.puzzles FAQ for more information.
  12. Sender: chris@questrel.com (Chris Cole)
  13. Reply-To: archive-comment@questrel.com
  14. Organization: Questrel, Inc.
  15. References: <puzzles/archive/Instructions_745653851@questrel.com>
  16. Date: Wed, 18 Aug 1993 06:06:09 GMT
  17. Approved: news-answers-request@MIT.Edu
  18. Expires: Thu, 1 Sep 1994 06:04:11 GMT
  19. Lines: 1393
  20. Xref: senator-bedfellow.mit.edu rec.puzzles:25006 news.answers:11526 rec.answers:1926
  21.  
  22. Archive-name: puzzles/archive/logic/part1
  23. Last-modified: 17 Aug 1993
  24. Version: 4
  25.  
  26.  
  27. ==> logic/29.p <==
  28. Three people check into a hotel.  They pay $30 to the manager and go
  29. to their room.  The manager finds out that the room rate is $25 and
  30. gives $5 to the bellboy to return.  On the way to the room the bellboy
  31. reasons that $5 would be difficult to share among three people so
  32. he pockets $2 and gives $1 to each person.
  33.  
  34. Now each person paid $10 and got back $1.  So they paid $9 each,
  35. totalling $27.  The bellboy has $2, totalling $29.
  36.  
  37. Where is the remaining dollar?
  38.  
  39. ==> logic/29.s <==
  40. Each person paid $9, totalling $27.  The manager has $25 and the bellboy $2.
  41. The bellboy's $2 should be added to the manager's $25 or subtracted from
  42. the tenants' $27, not added to the tenants' $27.
  43.  
  44. ==> logic/ages.p <==
  45. 1) Ten years from now Tim will be twice as old as Jane was when Mary was 
  46.    nine times as old as Tim.
  47.  
  48. 2) Eight years ago, Mary was half as old as Jane will be when Jane is one year
  49.    older than Tim will be at the time when Mary will be five times as old as 
  50.    Tim will be two years from now.
  51.  
  52. 3) When Tim was one year old, Mary was three years older than Tim will be when 
  53.    Jane is three times as old as Mary was six years before the time when Jane 
  54.    was half as old as Tim will be when Mary will be ten years older than Mary 
  55.    was when Jane was one-third as old as Tim will be when Mary will be three 
  56.    times as old as she was when Jane was born.
  57.  
  58.                              HOW OLD ARE THEY NOW?
  59.  
  60. ==> logic/ages.s <==
  61. The solution: Tim is 3, Jane is 8, and Mary is 15.  A little grumbling
  62. is in order here, as clue number 1 leads to the situation a year and a
  63. half ago, when Tim was 1 1/2, Jane was 6 1/2, and Mary was 13 1/2.
  64.  
  65. This sort of problem is easy if you write down a set of equations.  Let
  66. t be the year that Tim was born, j be the year that Jane was born, m be
  67. the year that Mary was born, and y be the current year.  As indefinite
  68. years come up, let y1, y2, ... be the indefinite years.  Then the
  69. equations are
  70.  
  71.  
  72. y + 10 - t = 2 (y1 - j)
  73. y1 - m = 9 (y1 - t)
  74.  
  75. y - 8 - m = 1/2 (y2 - j)
  76. y2 - j = 1 + y3 - t
  77. y3 - m = 5 (y + 2 - t)
  78.  
  79. t + 1 - m = 3 + y4 - t
  80. y4 - j = 3 (y5 - 6 - m)
  81. y5 - j = 1/2 (y6 - t)
  82. y6 - m = 10 + y7 - m
  83. y7 - j = 1/3 (y8 - t)
  84. y8 - m = 3 (j - m)
  85.  
  86. t = y - 3
  87. j = y - 8
  88. m = y - 15
  89.  
  90. ==> logic/attribute.p <==
  91. All the items in the first list share a particular attribute.  The second
  92. list is of some items lacking the attribute.
  93.  
  94. Set#1
  95.     with:  battery, key, yeast, bookmark
  96.     w/out: stapler, match, Rubik's cube, pill bottle
  97.  
  98. Set#2
  99.     with:  Rubik's cube, chess set, electrical wiring, compass needle
  100.     w/out: clock, rope, tic-tac-toe, pencil sharpener
  101.  
  102. Set#3:
  103.     with:  koosh, small intestine, Yorkshire Terrier, Christmas Tree
  104.     w/out: toothbrush, oak chair, soccer ball, icicle
  105.  
  106. Points to realize:
  107. 1.
  108.  There may be exceptions to any item on the list, for instance a particular
  109.  clock may share the properties of the 'with' list of problem two, BUT MOST
  110.  ORDINARY clocks do not.  All the properties apply the vast majority of the
  111.  the items mentioned.  Extraordinary exceptions should be ignored.
  112.  
  113. 2.
  114.  Pay the most attention to the 'with' list.  The 'without' list is only
  115.  present to eliminate various 'stupid' answers.
  116.  
  117. ==> logic/attribute.s <==
  118. The attribute puzzle format is a traditional format in math education.
  119. It occurs in logic materials developed in the sixties by EDC in Boston,
  120. with visual objects. Example:
  121.  
  122. these are gloops: A B C D E 
  123. these are not gloops: F G J L N
  124. which of these are gloops? O P Q R S
  125.  
  126. Set#1
  127.     with:  battery, key, yeast, bookmark
  128.     w/out: stapler, match, Rubik's cube, pill bottle
  129.  
  130. Needs to be placed inside something else when used for its usual purpose.
  131.  
  132. Set#2
  133.     with:  Rubik's cube, chess set, electrical wiring, compass needle
  134.     w/out: clock, rope, tic-tac-toe, pencil sharpener
  135.  
  136. Uses color to distinguish between otherwise identical parts.
  137.  
  138. Set#3:
  139.     with:  koosh, small intestine, Yorkshire Terrier, Christmas Tree
  140.     w/out: toothbrush, oak chair, soccer ball, icicle
  141.  
  142. Villiform.
  143.  
  144. ==> logic/bookworm.p <==
  145. A bookworm eats from the first page of an encyclopedia to the last
  146. page.  The bookworm eats in a straight line.  The encyclopedia consists
  147. of ten 1000-page volumes and is sitting on a bookshelf in the usual
  148. order.  Not counting covers, title pages, etc., how many pages does the
  149. bookworm eat through?
  150.  
  151. ==> logic/bookworm.s <==
  152. On a book shelf the first page of the first volume is on the "inside"
  153.   __                             __
  154. B|  |                           |  |F
  155. A|1 |...........................|10|R
  156. C|  |                           |  |O
  157. K|  |                           |  |N
  158.  |  |                           |  |T
  159.   ----------------------------------
  160. so the bookworm eats only through the cover of the first volume, then 8 times
  161. 1000 pages of Volumes 2 - 9, then through the cover to the 1st page of Vol 10.
  162. He eats 8,000 pages.
  163.  
  164. If the bookworm ate the first page and the last page, it ate 8,004 pages.
  165.  
  166. ==> logic/boxes.p <==
  167. Which Box Contains the Gold?
  168.  
  169. Two boxes are labeled "A" and "B".  A sign on box A says "The sign
  170. on box B is true and the gold is in box A".  A sign on box B says
  171. "The sign on box A is false and the gold is in box A".  Assuming there
  172. is gold in one of the boxes, which box contains the gold?
  173.  
  174. ==> logic/boxes.s <==
  175. The problem cannot be solved with the information given.
  176.  
  177. The sign on box A says "The sign on box B is true and the gold is in box A".
  178. The sign on box B says "The sign on box A is false and the gold is in box A".
  179. The following argument can be made: If the statement on box A is true, then
  180. the statement on box B is true, since that is what the statement on box A
  181. says.  But the statement on box B states that the statement on box A is false,
  182. which contradicts the original assumption.  Therefore, the statement on box A
  183. must be false.  This implies that either the statement on box B is false or
  184. that the gold is in box B.  If the statement on box B is false, then either
  185. the statement on box A is true (which it cannot be) or the gold is in box B.
  186. Either way, the gold is in box B.
  187.  
  188. However, there is a hidden assumption in this argument: namely, that
  189. each statement must be either true or false.  This assumption leads to
  190. paradoxes, for example, consider the statement: "This statement is
  191. false."  If it is true, it is false; if it is false, it is true.  The
  192. only way out of the paradox is to deny that the statement is either true
  193. or false and label it meaningless instead.  Both of the statements on the
  194. boxes are therefore meaningless and nothing can be concluded from them.
  195.  
  196. In general, statements about the truth of other statements lead to
  197. contradictions.  Tarski invented metalanguages to avoid this problem.
  198. To avoid paradox, a statement about the truth of a statement in a language
  199. must be made in the metalanguage of the language.
  200.  
  201. Common sense dictates that this problem cannot be solved with the information
  202. given.  After all, how can we deduce which box contains the gold simply by
  203. reading statements written on the outside of the box?  Suppose we deduce that
  204. the gold is in box B by whatever line of reasoning we choose.  What is to stop
  205. us from simply putting the gold in box A, regardless of what we deduced?
  206. (cf. Smullyan, "What Is the Name of This Book?", Prentice-Hall, 1978,    #70)
  207.  
  208. ==> logic/camel.p <==
  209. An Arab sheikh tells his two sons to race their camels to a distant
  210. city to see who will inherit his fortune.  The one whose camel is
  211. slower will win.  The brothers, after wandering aimlessly for days, ask
  212. a wise man for advise.  After hearing the advice they jump on the
  213. camels and race as fast as they can to the city.  What does the wise
  214. man say?
  215.  
  216. ==> logic/camel.s <==
  217. The wiseman tells them to switch camels.
  218.  
  219. ==> logic/centrifuge.p <==
  220. You are a biochemist, working with a 12-slot centrifuge.  This is a gadget
  221. that has 12 equally spaced slots around a central axis, in which you can
  222. place chemical samples you want centrifuged.  When the machine is turned on,
  223. the samples whirl around the central axis and do their thing.
  224.  
  225. To ensure that the samples are evenly mixed, they must be distributed in the
  226. 12 slots such that the centrifuge is balanced evenly.  For example, if you
  227. wanted to mix 4 samples, you could place them in slots 12, 3, 6 and 9
  228. (assuming the slots are numbered from 1 to 12 like a clock).
  229.  
  230. Problem:  Can you use the centrifuge to mix 5 samples?
  231.  
  232. ==> logic/centrifuge.s <==
  233. The superposition of any two solutions is yet another solution, so given
  234. that the factors > 1 of 12 (2, 3, 4, 6, 12) are all solutions, the
  235. only thing to check about, for example, the proposed solution 2+3 is
  236. that not all ways of combining 2 & 3 would have centrifuge tubes
  237. from one subsolution occupying the slot for one of the tubes in
  238. another solution.  For the case 2+3, there is no problem:  Place 3
  239. tubes, one in every 4th position, then place the 4th and 5th
  240. diametrically opposed (each will end up in a slot adjacent to one of
  241. the first 3 tubes).  The obvious generalization is, what are the
  242. numbers of tubes that cannot be balanced?  Observing that there are
  243. solutions for 2,3,4,5,6 tubes and that if X has a solution, 12-X has
  244. also one (obtained by swapping tubes and holes), it is obvious that
  245. 1 and 11 are the only cases without solutions.
  246.  
  247. Here is how this problem is often solved in practice:  A dummy tube
  248. is added to produce a total number of tubes that is easy to balance.
  249. For example, if you had to centrifuge just one sample, you'd add a
  250. second tube opposite it for balance.
  251.  
  252. ==> logic/chain.p <==
  253. What is the least number of links you can cut in a chain of 21 links to be able
  254. to give someone all possible number of links up to 21?
  255.  
  256. ==> logic/chain.s <==
  257. Two.
  258.  
  259.     OOO C OOOOO C OOOOOOOOOOO
  260. (where Os are chained unbroken links, and the Cs are the unchained broken links)
  261.  
  262. And equivalently:
  263.  
  264.     OOO C OOOOOO C OOOOOOOOOO
  265.  
  266. ==> logic/children.p <==
  267. A man walks into a bar, orders a drink, and starts chatting with the
  268. bartender.  After a while, he learns that the bartender has three
  269. children.  "How old are your children?" he asks.  "Well," replies the
  270. bartender, "the product of their ages is 72."  The man thinks for a
  271. moment and then says, "that's not enough information."  "All right,"
  272. continues the bartender, "if you go outside and look at the building
  273. number posted over the door to the bar, you'll see the sum of the
  274. ages."  The man steps outside, and after a few moments he reenters and
  275. declares, "Still not enough!"  The bartender smiles and says, "My
  276. youngest just loves strawberry ice cream."
  277.  
  278. How old are the children?
  279.  
  280. A variant of the problem is for the sum of the ages to be 13 and the
  281. product of the ages to be the number posted over the door.  In this
  282. case, it is the oldest that loves ice cream.
  283.  
  284. Then how old are they?
  285.  
  286.  
  287. ==> logic/children.s <==
  288. First, determine all the ways that three ages can multiply together to get 72:
  289.  
  290. 72  1  1        (quite a feat for the bartender)
  291. 36  2  1
  292. 24  3  1
  293. 18  4  1
  294. 18  2  2
  295. 12  6  1
  296. 12  3  2
  297. 9  4  2
  298. 9  8  1
  299. 8  3  3
  300. 6  6  2
  301. 6  4  3
  302.  
  303. As the man says, that's not enough information; there are many possibilities.
  304. So the bartender tells him where to find the sum of the ages--the man now knows
  305. the sum even though we don't.  Yet he still insists that there isn't enough
  306. info.  This must mean that there are two permutations with the same sum;
  307. otherwise the man could have easily deduced the ages.
  308.  
  309. The only pair of permutations with the same sum are 8 3 3 and 6 6 2, which both
  310. add up to 14 (the bar's address).  Now the bartender mentions his
  311. "youngest"--telling us that there is one child who is younger than the other
  312. two.  This is impossible with 8 3 3--there are two 3 year olds.  Therefore the
  313. ages of the children are 6, 6, and 2.
  314.  
  315. Pedants have objected that the problem is insoluble because there could be
  316. a youngest between two three year olds (even twins are not born exactly at
  317. the same time).  However, the word "age" is frequently used to denote the
  318. number of years since birth.  For example, I am the same age as my wife,
  319. even though technically she is a few months older than I am.  And using the
  320. word "youngest" to mean "of lesser age" is also in keeping with common parlance.
  321. So I think the solution is fine as stated.
  322.  
  323. In the sum-13 variant, the possibilities are:
  324.  
  325. 11  1  1
  326. 10  2  1
  327. 9  3  1
  328. 9  2  2
  329. 8  4  1
  330. 8  3  2
  331. 7  5  1
  332. 7  4  2
  333. 7  3  3
  334. 6  6  1
  335. 6  5  2
  336. 6  4  3
  337.  
  338. The two that remain are 9 2 2 and 6 6 1 (both products equal 36).  The
  339. final bit of info (oldest child) indicates that there is only one
  340. child with the highest age.  This cancels out the 6 6 1 combination, leaving
  341. the childern with ages of 9, 2, and 2.
  342.  
  343. ==> logic/condoms.p <==
  344. How can a man have mutually safe sex with three women with only two condoms?
  345. How can three men have mutually safe sex with one woman with two condoms?
  346.  
  347. ==> logic/condoms.s <==
  348. Use both condoms on the first woman.  Take off the outer condom (turning it
  349. inside-out in the process) and set it aside.  Use the inner condom alone on
  350. the second woman.  Put the outer condom back on.  Use it on the third woman.
  351.  
  352. First man uses both condoms.  Take off the outer condom (do NOT reverse
  353. it) and have second man use it.  First man takes off the inner condom
  354. (turning it inside-out).  Third man puts on this condom, followed by
  355. second man's condom.
  356.  
  357. ==> logic/dell.p <==
  358. How can I solve logic puzzles (e.g., as published by Dell) automatically?
  359.  
  360. ==> logic/dell.s <==
  361. #include    <setjmp.h>
  362.  
  363. #define    EITHER        if (S[1] = S[0], ! setjmp((S++)->jb)) {
  364. #define    OR        } else EITHER
  365. #define    REJECT        longjmp((--S)->jb, 1)
  366. #define    END_EITHER    } else REJECT;
  367.  
  368. /* values in tmat: */
  369. #define    T_UNK    0
  370. #define    T_YES    1
  371. #define    T_NO    2
  372.  
  373. #define    Val(t1,t2)    (S->tmat[t1][t2])
  374. #define    CLASS(x)        \
  375.         (((x) / NUM_ITEM) * NUM_ITEM)
  376. #define    EVERY_TOKEN(x)        \
  377.         (x = 0; x < TOT_TOKEN; x++)
  378. #define    EVERY_ITEM(x, class)    \
  379.         (x = CLASS(class); x < CLASS(class) + NUM_ITEM; x++)
  380.  
  381. #define    BEGIN                        \
  382. struct state {                        \
  383.     char    tmat[TOT_TOKEN][TOT_TOKEN];        \
  384.     jmp_buf jb;                    \
  385. } States[100], *S = States;                \
  386.                             \
  387. main()                            \
  388. {                            \
  389.     int    token;                    \
  390.                             \
  391.     for EVERY_TOKEN(token)                \
  392.         yes(token, token);            \
  393.     EITHER
  394.  
  395. /* Here is the problem-specific data */
  396. #define    NUM_ITEM    5
  397. #define    NUM_CLASS    6
  398. #define    TOT_TOKEN    (NUM_ITEM * NUM_CLASS)
  399.  
  400. #define    HOUSE_0        0
  401. #define    HOUSE_1        1
  402. #define    HOUSE_2        2
  403. #define    HOUSE_3        3
  404. #define    HOUSE_4        4
  405.  
  406. #define    ENGLISH        5
  407. #define    SPANISH        6
  408. #define    NORWEG        7
  409. #define    UKRAIN        8
  410. #define    JAPAN        9
  411.  
  412. #define    GREEN        10
  413. #define    RED        11
  414. #define    IVORY        12
  415. #define    YELLOW        13
  416. #define    BLUE        14
  417.  
  418. #define    COFFEE        15
  419. #define    TEA        16
  420. #define    MILK        17
  421. #define    OJUICE        18
  422. #define    WATER        19
  423.  
  424. #define    DOG        20
  425. #define    SNAIL        21
  426. #define    FOX        22
  427. #define    HORSE        23
  428. #define    ZEBRA        24
  429.  
  430. #define    OGOLD        25
  431. #define    PLAYER        26
  432. #define    CHESTER        27
  433. #define    LSTRIKE        28
  434. #define    PARLIA        29
  435.  
  436. char *names[] = {
  437.     "HOUSE_0", "HOUSE_1", "HOUSE_2", "HOUSE_3", "HOUSE_4",
  438.     "ENGLISH", "SPANISH", "NORWEG", "UKRAIN", "JAPAN",
  439.     "GREEN", "RED", "IVORY", "YELLOW", "BLUE",
  440.     "COFFEE", "TEA", "MILK", "OJUICE", "WATER",
  441.     "DOG", "SNAIL", "FOX", "HORSE", "ZEBRA",
  442.     "OGOLD", "PLAYER", "CHESTER", "LSTRIKE", "PARLIA",
  443. };
  444.  
  445.     BEGIN
  446.  
  447.     yes(ENGLISH, RED);    /* Clue 1 */
  448.     yes(SPANISH, DOG);    /* Clue 2 */
  449.     yes(COFFEE, GREEN);    /* Clue 3 */
  450.     yes(UKRAIN, TEA);    /* Clue 4 */
  451.  
  452.     EITHER            /* Clue 5 */
  453.         yes(IVORY, HOUSE_0);
  454.         yes(GREEN, HOUSE_1);
  455.     OR
  456.         yes(IVORY, HOUSE_1);
  457.         yes(GREEN, HOUSE_2);
  458.     OR
  459.         yes(IVORY, HOUSE_2);
  460.         yes(GREEN, HOUSE_3);
  461.     OR
  462.         yes(IVORY, HOUSE_3);
  463.         yes(GREEN, HOUSE_4);
  464.     END_EITHER
  465.  
  466.     yes(OGOLD, SNAIL);    /* Clue 6 */
  467.     yes(PLAYER, YELLOW);    /* Clue 7 */
  468.     yes(MILK, HOUSE_2);    /* Clue 8 */
  469.     yes(NORWEG, HOUSE_0);    /* Clue 9 */
  470.  
  471.     EITHER            /* Clue 10 */
  472.         yes(CHESTER, HOUSE_0);
  473.         yes(FOX, HOUSE_1);
  474.     OR
  475.         yes(CHESTER, HOUSE_4);
  476.         yes(FOX, HOUSE_3);
  477.     OR
  478.         yes(CHESTER, HOUSE_1);
  479.         EITHER    yes(FOX, HOUSE_0);
  480.         OR    yes(FOX, HOUSE_2);
  481.         END_EITHER
  482.     OR
  483.         yes(CHESTER, HOUSE_2);
  484.         EITHER    yes(FOX, HOUSE_1);
  485.         OR    yes(FOX, HOUSE_3);
  486.         END_EITHER
  487.     OR
  488.         yes(CHESTER, HOUSE_3);
  489.         EITHER    yes(FOX, HOUSE_2);
  490.         OR    yes(FOX, HOUSE_4);
  491.         END_EITHER
  492.     END_EITHER
  493.  
  494.     EITHER            /* Clue 11 */
  495.         yes(PLAYER, HOUSE_0);
  496.         yes(HORSE, HOUSE_1);
  497.     OR
  498.         yes(PLAYER, HOUSE_4);
  499.         yes(HORSE, HOUSE_3);
  500.     OR
  501.         yes(PLAYER, HOUSE_1);
  502.         EITHER    yes(HORSE, HOUSE_0);
  503.         OR    yes(HORSE, HOUSE_2);
  504.         END_EITHER
  505.     OR
  506.         yes(PLAYER, HOUSE_2);
  507.         EITHER    yes(HORSE, HOUSE_1);
  508.         OR    yes(HORSE, HOUSE_3);
  509.         END_EITHER
  510.     OR
  511.         yes(PLAYER, HOUSE_3);
  512.         EITHER    yes(HORSE, HOUSE_2);
  513.         OR    yes(HORSE, HOUSE_4);
  514.         END_EITHER
  515.     END_EITHER
  516.  
  517.     yes(LSTRIKE, OJUICE);    /* Clue 12 */
  518.     yes(JAPAN, PARLIA);    /* Clue 13 */
  519.  
  520.     EITHER            /* Clue 14 */
  521.         yes(NORWEG, HOUSE_0);
  522.         yes(BLUE, HOUSE_1);
  523.     OR
  524.         yes(NORWEG, HOUSE_4);
  525.         yes(BLUE, HOUSE_3);
  526.     OR
  527.         yes(NORWEG, HOUSE_1);
  528.         EITHER    yes(BLUE, HOUSE_0);
  529.         OR    yes(BLUE, HOUSE_2);
  530.         END_EITHER
  531.     OR
  532.         yes(NORWEG, HOUSE_2);
  533.         EITHER    yes(BLUE, HOUSE_1);
  534.         OR    yes(BLUE, HOUSE_3);
  535.         END_EITHER
  536.     OR
  537.         yes(NORWEG, HOUSE_3);
  538.         EITHER    yes(BLUE, HOUSE_2);
  539.         OR    yes(BLUE, HOUSE_4);
  540.         END_EITHER
  541.     END_EITHER
  542.  
  543. /* End of problem-specific data */
  544.  
  545.         solveit();
  546.     OR
  547.         printf("All solutions found\n");
  548.         exit(0);
  549.     END_EITHER
  550. }
  551.  
  552. no(a1, a2)
  553. {
  554.     int    non1, non2, token;
  555.  
  556.     if (Val(a1, a2) == T_YES)
  557.         REJECT;
  558.     else if (Val(a1, a2) == T_UNK) {
  559.         Val(a1, a2) = T_NO;
  560.         no(a2, a1);
  561.         non1 = non2 = -1;
  562.  
  563.         for EVERY_ITEM(token, a1)
  564.             if (Val(token, a2) != T_NO)
  565.                 if (non1 == -1)
  566.                     non1 = token;
  567.                 else
  568.                     break;
  569.         if (non1 == -1)
  570.             REJECT;
  571.         else if (token == CLASS(a1) + NUM_ITEM)
  572.             yes(non1, a2);
  573.  
  574.         for EVERY_TOKEN(token)
  575.             if (Val(token, a1) == T_YES)
  576.                 no(a2, token);
  577.     }
  578. }
  579.  
  580. yes(a1, a2)
  581. {
  582.     int    token;
  583.  
  584.     if (Val(a1, a2) == T_NO)
  585.         REJECT;
  586.     else if (Val(a1, a2) == T_UNK) {
  587.         Val(a1, a2) = T_YES;
  588.         yes(a2, a1);
  589.         for EVERY_ITEM(token, a1)
  590.             if (token != a1)
  591.                 no(token, a2);
  592.         for EVERY_TOKEN(token)
  593.             if (Val(token, a1) == T_YES)
  594.                 yes(a2, token);
  595.             else if (Val(token, a1) == T_NO)
  596.                 no(a2, token);
  597.     }
  598. }
  599.  
  600. solveit()
  601. {
  602.     int    token, tok2;
  603.  
  604.     for EVERY_TOKEN(token)
  605.         for (tok2 = token; tok2 < TOT_TOKEN; tok2++)
  606.             if (Val(token, tok2) == T_UNK) {
  607.                 EITHER
  608.                     yes(token, tok2);
  609.                 OR
  610.                     no(token, tok2);
  611.                 END_EITHER;
  612.                 return solveit();
  613.             }
  614.     printf("Solution:\n");
  615.     for EVERY_ITEM(token, 0) {
  616.         for (tok2 = NUM_ITEM; tok2 < TOT_TOKEN; tok2++)
  617.             if (Val(token, tok2) == T_YES)
  618.                 printf("\t%s %s\n",names[token],names[tok2]);
  619.         printf("\n");
  620.     }
  621.     REJECT;
  622. }
  623.  
  624. ---
  625. james@crc.ricoh.com (James Allen)
  626.  
  627. ==> logic/elimination.p <==
  628. 97 baseball teams participate in an annual state tournament.
  629. The way the champion is chosen for this tournament is by the same old
  630. elimination schedule. That is, the 97 teams are to be divided into
  631. pairs, and the two teams of each pair play against each other.
  632. After a team is eliminated from each pair, the winners would
  633. be again divided into pairs, etc.  How many games must be played
  634. to determine a champion?
  635.  
  636. ==> logic/elimination.s <==
  637. In order to determine a winner all but one team must lose.
  638. Therefore there must be at least 96 games.
  639.  
  640. ==> logic/flip.p <==
  641. How can a toss be called over the phone (without requiring trust)?
  642.  
  643. ==> logic/flip.s <==
  644. A flips a coin.  If the result is heads, A multiplies 2 prime numbers
  645. containing about 90 digits; if the result is tails, A multiplies 3
  646. prime numbers containing about 60 digits.  A tells B the result of the
  647. multiplication.  B now calls either heads or tails and tells A.  A then
  648. supplies B with the original numbers to verify the flip.
  649.  
  650. Consider what is involved in multiplying 90 digit numbers.  Using the method
  651. of long multiplication that we all learned in grade school, you have maybe
  652. 90 or so strings of 90 characters (or "digits") each.  That's no problem for
  653. a computer to deal with.  The magnitude of the number represented isn't
  654. much of a factor; we're only manipulating the string of digits.
  655.  
  656. Consider what is involved in factoring 90 digit numbers.  There are of course,
  657. little tricks for determining factorability by small integers which we all
  658. learned in grade school.  (Is the last digit even?  Is the sum of all the
  659. digits divisible by 9?  And so on.)  But these are of little use in factoring
  660. large numbers with large factors.  In fact, there is no essentially better
  661. method than checking every number smaller that the number to be factored and
  662. seeing if it one divides the other evenly.  We means we could be checking some
  663. 100,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
  664. nummbers.  This is very hard to do, even for a supercomputer.  Here, the
  665. number of digits this number has is of little consequence.  It is the
  666. magnitude of the number that we have to worry about, and there just isn't 
  667. enough time in the world to do this properly.
  668.  
  669. Where does A find a list of 60- and 90- digit prime numbers?  Well, that's not
  670. to hard to come by.  The simplest method to find a few prime numbers is simply
  671. to choose a 90-digit number (or 60-digit number, as the case warrants)
  672. randomly, and check to see if it is prime.  You won't have to wait too long
  673. before you stumble across a prime number.
  674.  
  675. "But wait!" you cry.  "I thought you just said it was incredibly difficult
  676. to factor large numbers.  If that's the case, then how are you going to know
  677. if the number you randomly choose is prime?"  A good question.  Here we enter
  678. into the strange an wacky world of number theory.  It turns out (and I won't
  679. explain how unless someone asks) there are "probabalistic" algorithms,
  680. which depend on us choosing numbers at random.  There are probablistic
  681. algorithms that when given a prime number, will quickly tell us that it is
  682. a prime number, and when given a composite number, will either tell us that
  683. it is a composite number (with very, very high probability) or will tell us
  684. that it is a prime number (with very, very low probability.)  What's the use
  685. of an algorithm that only returns the right answer "with very, very high
  686. probability?"  Well, we can make this probability as high as we want, just by
  687. running the algorithm longer.  In fact, it is within our technological
  688. abilities to quickly run this algorithm for 90-digit numbers so that the
  689. probability of it giving a wrong answer is less than the probability of a
  690. cosmic ray striking a vital part of the computer at some vital time and causing
  691. the computer to spit out the wrong answer anyway.  That's what I mean by "very,
  692. very high."
  693.  
  694. ==> logic/flowers.p <==
  695. How many flowers do I have if all of them are roses except two, all of
  696. them are tulips except two, and all of them are daisies except two?
  697.  
  698. ==> logic/flowers.s <==
  699. There are two solutions:
  700.  
  701. Three flowers: rose, tulip, daisy
  702. Two flowers:   carnation, geranium
  703.  
  704. ==> logic/friends.p <==
  705. Any group of 6 or more contains either 3 mutual friends or 3 mutual strangers.
  706. Prove it.
  707.  
  708. ==> logic/friends.s <==
  709. Take a person X.  Of the five other people, there must either be at least three
  710. acquaintances of X or at least three strangers of X.  Assume wlog that X has
  711. three strangers A,B,C.  Unless A,B,C is the required triad of acquaintances,
  712. they must include a pair of strangers, wlog A,B.  Then X,A,B is the required
  713. triad of strangers, QED.
  714.  
  715. ==> logic/hofstadter.p <==
  716. In first-order logic, find a predicate P(x) which means "x is a power of 10."
  717.  
  718. ==> logic/hofstadter.s <==
  719. Well, one summer, I decided to tackle the problem. Not having any great
  720. knowledge of number theory, I used a more brute force approach. Note
  721. that for greater comprehensibility, I have broken the resulting formula
  722. up into several stages, but it would not be difficult to put it
  723. back together into one vast formula:
  724.  
  725. {e is prime:}
  726. PRIME(e) := 
  727.    ~Eb:Ec:((b+2)*(c+2) = e)
  728.  
  729. {if e is a prime, true iff a is a power of e:}
  730. PPOWER(a,e) :=
  731.    Ab:Ac:((b*c = a) -> ((b = 1) or (Ed: (e*d) = b)))
  732.  
  733. {if e is a prime, and a is a power of e, true iff d is the
  734.  (log_e a)th digit (counting from the right, starting with 0)
  735.  in the base-e expansion of n:}
  736. DIG(a,e,d,n) :=
  737.    (d < e) & Eb:Ec:((c < a) & (n = (b*e*a) + (d*a) + c))
  738.  
  739. {if e is prime, and a is a power of e, true iff n has exactly
  740.  (log_e a) digits in its base-e expansion (0 is counted as having 0
  741.  digits:}
  742. LENGTH(e,a,n):=
  743.    (n < a) & Ab:((PPOWER(b,e) & (b < a)) -> (b <= n))
  744.  
  745. POWER_OF_TEN(x):=
  746.    Ee:(PRIME(e) & (e > x) &
  747.        En:Ea:(LENGTH(e,a,n) &
  748.               DIG(1,e,1,n) &
  749.               Ai:Aj:Au:(  (PPOWER(u,e) & ((e*u) < a)
  750.                & DIG(u,e,i,n) & DIG(e*u,e,j,n))
  751.                           -> j = (10 * i)  ) &
  752.               Eu:(PPOWER(u,e) & (e*u = a) & DIG(u,e,x,n))
  753.        )     )
  754.  
  755. The basic idea is that you are asserting that for some prime e greater
  756. than x, we can find a number n which, when expressed in base-e notation,
  757. will have 1 in its units place, 10 in its e's place, 100 in its (e^2)'s
  758. place, and in general have the "digit" in each place be 10 times
  759. greater than the one to its right, such that the leftmost digit is our x.
  760.  
  761. To translate into Hofstadter's notation, of course, we must use horse-shoes
  762. instead of ->'s, big carets instead of &'s, letters a through e (followed
  763. by however many ''s) exclusively, and so forth. (We must also replace <'s
  764. and <= with appropriate expansions, and substitute in for our capitalized
  765. formula abbreviations.) This is left as an exercise to the reader.
  766.  
  767. Kevin Wald
  768. wald2@husc.harvard.edu
  769.  
  770. ==> logic/hundred.p <==
  771. A sheet of paper has statements numbered from 1 to 100.  Statement n says
  772. "exactly n of the statements on this sheet are false."  Which statements are
  773. true and which are false?  What if we replace "exactly" by "at least"?
  774.  
  775. ==> logic/hundred.s <==
  776. From _Litton's Problematical Recreations_
  777.  
  778. It is tempting to argue as follows:
  779.  
  780. At most one statement can be true (they are mutually contradictory).
  781. If they are all false, statement 100 would be true, which is no good.
  782. So 99 are false, and statement 99 is true.
  783.  
  784. If replaced by "at least", and the "real" number of false statements is
  785. x, then statements x+1 to 100 will be false (since they falsely claim
  786. that there are more false statements than there actually are). So, 100-x are 
  787. false, ie.  x=100-x, so x=50. The first 50 statements are true, and statements
  788. 51 to 100 are false. 
  789.  
  790. However, there is a hidden and incorrect assumption in this argument.
  791. To see this, suppose that there is one statement on the sheet and it
  792. says "One statement is false"  or "At least one statement is false,"
  793. either way it implies "this statement is false," which is a familiar
  794. paradoxical statement.  We have learned that this paradox arises because
  795. of the false assumption that all statements are either true or false.
  796. This is the hidden assumption in the above reasoning.
  797.  
  798. If it is acknowledged that some of the statements on the page may be
  799. neither true nor false (i.e., meaningless), then nothing whatsoever can
  800. be concluded about which statements are true or false.
  801.  
  802. This problem has been carefully contrived to appear to be solvable (like
  803. the vacuous statement "this statement is true").  By changing the
  804. numbers in some statements and changing "true" to "false," various
  805. circular forms of the liar's paradox can be constructed.  A much
  806. more complicated version of the same problem is:
  807.  
  808.  1) At least one of the last two statements in this list is true.
  809.  
  810.  2) This is either the first true or the first false statement in the
  811.     list.
  812.  
  813.  3) There exist three consecutive false statements.
  814.  
  815.  4) The difference beween the numbers of the last true statement and
  816.     the first true statement is a factor of the unknown number.
  817.  
  818.  5) The sum of the numbers of the true statements is the unknown
  819.     number.
  820.  
  821.  6) This is not the last true statement.
  822.  
  823.  7) Each true statement's number is a factor of the unknown number.
  824.  
  825.  8) The unknown number equals the percentage of these statements which
  826.    are true.
  827.  
  828.  9) The number of different factors which the unknown number has
  829.    (excluding 1 and itself) is more than the sum of the numbers of the
  830.    true statements.
  831.  
  832. 10) There are no three cosecutive true statements.
  833.  
  834. What is the number?
  835.  
  836. The incorrect but plausible solution is:
  837.  
  838. By 2, either way 1 must be false, and then so must both 9 and 10.
  839.  
  840. 6, if false, says "This is the last true statement", which gives
  841. a paradox, thus 6 must be true, and so must 7 and/or 8.
  842.  
  843. 7 and 8 cannot both be true, as the number had to be a multiple
  844. of 6,7,8 , that is a multiple of 168 (by 7), and less than 100 (by 8)
  845.  
  846. If 8 is false, then 3 is true (8,9,10 is false), if 8 is true, then
  847. 3 is false (3 cannot be true when both 6 and 8 are true, then there
  848. are no three consecutive statements left).
  849.  
  850. So we have either
  851.  
  852. A) F X F X X T F T F F
  853. or
  854. B) F X T X X T T F F F
  855.  
  856. In A), 4 and 5 must be true (by false 10), and 2 may be true or false.
  857. So by 5 the number shall be either 27 (2+3+4+5+6+7) or 25 (3+4+5+6+7).
  858. None of these can fullfill 8, though, so A) is out leaving us with B)
  859.  
  860. Now, (by 7) 3,6 and 7 are factors, the number must be a multiple of
  861. 42, as 2+3+4+5+6+7=27, 5 must be false. 
  862.  
  863. By false 10, 2 and 4 must be true, that is 5 shall be a multiple
  864. of the number. Now the number must be a multiple of 2,3,4,5,6 and 7,
  865. that is a multiple of 3*4*5*7=420. 420 has 22 different factors
  866. (2,3,4,5,6,7,10,12,14,15,20,21,28,30,35,42,60,70,84,105,140,210)
  867. and the sum 2+3+4+6+7 = 22, so the only multiple of 420 that
  868. fulfills false 9 is 420.
  869.  
  870. ==> logic/inverter.p <==
  871. Can a digital logic circuit with two inverters invert N independent inputs?
  872. The circuit may contain any number of AND or OR gates.
  873.  
  874. ==> logic/inverter.s <==
  875. It can be shown that N inverters can invert 2^N-1 independent inputs,
  876. given an unlimited supply of AND and OR gates. The classic version of
  877. this puzzle is to invert 3 independent inputs using AND gates, OR
  878. gates, and only 2 inverters.
  879.  
  880. This is solved by:
  881.         n1 = not(i1 and i2 or i1 and i3 or i2 and i3);
  882.         n2 = not((i1 or i2 or i3) and n1  or  i1 and i2 and i3);
  883.         o1 = (i2 or i3 or n2) and n1  or  i2 and i3 and n2;
  884.         o2 = (i1 or i3 or n2) and n1  or  i1 and i3 and n2;
  885.         o3 = (i1 or i2 or n2) and n1  or  i1 and i2 and n2;
  886.  
  887. i1, i2, and i3 are the inputs, n1 and n2 are the inverted signals, and
  888. o1, o2, and o3 are the outputs.  "and" has higher precedence than "or".
  889.  
  890. So, start with N inverters.  Replace 3 of them with 2.
  891. Keep doing that until you're down to 2 inverters.
  892.  
  893. I was skeptical at first, because such a design requires so much feedback
  894. that I was sure the system would oscillate when switching between two
  895. particular states.  But after writing a program to test every possible state 
  896. change (32^2), it appears that this system settles after a maximum of
  897. 3 feedback logic iterations. I did not include gate delays in the simulation,
  898. however, which could increase the number of iterations before the system
  899. settles.
  900.  
  901. In any case, it appears that the world needs only 2 inverters! :-)
  902.  
  903. ==> logic/josephine.p <==
  904. The recent expedition to the lost city of Atlantis discovered scrolls
  905. attributted to the great poet, scholar, philosopher Josephine. They
  906. number eight in all, and here is the first.
  907.  
  908. THE KINGDOM OF MAMAJORCA, WAS RULED BY QUEEN HENRIETTA I. IN MAMAJORCA
  909. WOMEN HAVE TO PASS AN EXTENSIVE LOGIC EXAM BEFORE THEY ARE ALLOWED TO
  910. GET MARRIED. QUEENS DO NOT HAVE TO TAKE THIS EXAM. ALL THE WOMEN IN
  911. MAMAJORCA ARE LOYAL TO THEIR QUEEN AND DO WHATEVER SHE TELLS THEM TO.
  912. THE QUEENS OF MAMAJORCA ARE TRUTHFUL. ALL SHOTS FIRED IN MAMAJORCA CAN
  913. BE HEARD IN EVERY HOUSE. ALL ABOVE FACTS ARE KNOWN TO BE COMMON
  914. KNOWLEDGE.
  915.  
  916. HENRIETTA WAS WORRIED ABOUT THE INFIDELITY OF THE MARRIED MEN IN
  917. MAMAJORCA.  SHE SUMMONED ALL THE WIVES TO THE TOWN SQUARE, AND MADE
  918. THE FOLLOWING ANNOUNCEMENT. "THERE IS AT LEAST ONE UNFAITHFUL HUSBAND
  919. IN MAMAJORCA. ALL WIVES KNOW WHICH HUSBANDS ARE UNFAITHFUL, BUT HAVE
  920. NO KNOWLEDGE ABOUT THE FIDELITY OF THEIR OWN HUSBAND. YOU ARE
  921. FORBIDDEN TO DISCUSS YOUR HUSBAND'S FAITHFULNESS WITH ANY OTHER WOMAN.
  922. IF YOU DISCOVER THAT YOUR HUSBAND IS UNFAITHFUL, YOU MUST SHOOT HIM AT
  923. PRECISELY MIDNIGHT OF THE DAY YOU FIND THAT OUT."
  924.  
  925. THIRTY-NINE SILENT NIGHTS FOLLOWED THE QUEEN'S ANNOUNCEMENT. ON THE
  926. FORTIETH NIGHT, SHOTS WERE HEARD. QUEEN HENRIETTA I IS REVERED IN
  927. MAMAJORCAN HISTORY.
  928.  
  929. As with all philosophers Josephine doesn't provide the question, but leaves
  930. it implicit in his document. So figure out the questions - there are two -
  931. and answer them.
  932.  
  933. Here is Josephine's second scroll.
  934.  
  935. QUEEN HENRIETTA I WAS SUCCEEDED BY DAUGHTER QUEEN HENRIETTA II. AFTER
  936. A WHILE HENRIETTA LIKE HER FAMOUS MOTHER BECAME WORRIED ABOUT THE
  937. INFIDELITY PROBLEM. SHE DECIDED TO ACT, AND SENT A LETTER TO HER
  938. SUBJECTS (WIVES) THAT CONTAINED THE EXACT WORDS OF HENRIETTA I'S
  939. FAMOUS SPEECH.  SHE ADDED THAT THE LETTERS WERE GUARENTEED TO REACH
  940. ALL WIVES EVENTUALLY.
  941.  
  942. QUEEN HENRIETTA II IS REMEMBERED AS A FOOLISH AND UNJUST QUEEN.
  943.  
  944. What is the question and answer implied by this scroll?
  945.  
  946. ==> logic/josephine.s <==
  947. The two questions for scroll #1 were:
  948.  
  949. 1. How many husbands were shot on that fateful night?
  950. 2. Why is Queen Henrietta I revered in Mamajorca?
  951.  
  952. The answers are:
  953.  
  954. If there are n unfaithful husbands (UHs), every wife of an UH knows of
  955. n-1 UH's while every wife of a faithful husband knows of n UHs.  [this
  956. because everyone has perfect information about everything except the
  957. fidelity of their own husband].  Now we do a simple induction: Assume
  958. that there is only one UH.  Then all the wives but one know that there
  959. is just one UH, but the wife of the UH thinks that everyone is
  960. faithful.  Upon hearing that "there is at least one UH", the wife
  961. realizes that the only husband it can be is her own, and so shoots
  962. him.  Now, imagine that there are just two UH's.  Each wife of an UH
  963. assumes that the situation is "only one UH in town" and so waits to
  964. hear the other wife (she knows who it is, of course) shoot her husband
  965. on the first night.  When no one is shot, that can only be because her
  966. OWN husband was a second UH.  The wife of the second UH makes the same
  967. deduction when no shot is fired the first night (she was waiting, and
  968. expecting the other to shoot, too).  So they both figure it out after
  969. the first night, and shoot their husbands the second night.  It is
  970. easy to tidy up the induction to show that the n UHs will all be shot
  971. just on the n'th midnight.
  972.  
  973. The question for scroll #2 is:
  974.  
  975. 3. Why is Queen Henrietta II not?
  976.  
  977. The answer is:
  978.  
  979. The problem now is that QHII didn't realize that it is *critical* that
  980. all of the wives, of faithful and UH's alike, to
  981. *BEGIN*AT*THE*SAME*MOMENT*.  The uncertainty of having a particular
  982. wife's notice come a day or two late makes the whole logic path fall
  983. apart.  That's why she's foolish.  She is unjust, because some wives,
  984. honed and crack logicians all, remember, will *incorrectly* shoot
  985. faithful husbands.  Let us imagine the situation with just a SINGLE UH
  986. in the whole country.  And, wouldn't you know it, the notice to the
  987. wife of the UH just happens to be held up a day, whereas everyone
  988. else's arrived the first day.  Now, all of the wives that got the
  989. notice the first day know that there is just one UH in the country.
  990. And they know that the wife of that UH will think that everyone is
  991. faithful, and so they'll expect her to figure it out and shoot her
  992. husband the first night.  BUT SHE DIDN"T GET THE NOTICE THE FIRST
  993. NIGHT....  BUT THE OTHER WIVES HAVE NO WAY OF KNOWING THAT.  So, the
  994. wife of the UH doesn't know that anything is going on and so (of
  995. course) doesn't do anything the first night.  The next day she gets
  996. the notice, figures it all out, and her husband will be history come
  997. that midnight.  BUT... *every* other wife thought that there should
  998. have been a shooting the first night, and since there wasn't there
  999. must have been an additional UH, and it can only have been _her_
  1000. husband.  So on the second night **ALL** of the husbands are shot.
  1001. Things are much more complicated if the mix of who gets the notice
  1002. when is less simple than the one I mentioned above, but it is always
  1003. wrong and/or tragic.
  1004.  
  1005. NOTE: if the wives *know* that the country courier service (or however
  1006. these things get delivered) is flaky, then they can avoid the
  1007. massacre, but unless the wives exchange notes no one will ever be shot
  1008. (since there is always a chance that rather than _your_ husband being
  1009. an UH, you could reason that it might be that the wife of one of the
  1010. UH's that you know about just hasn't gotten her copy of the scroll
  1011. yet).  I guess you could call this case "unjust", too, since the UH's
  1012. evade punishment, despite the perfect logic of the wives.
  1013.  
  1014. ==> logic/locks.and.boxes.p <==
  1015. You want to send a valuable object to a friend.  You have a box which
  1016. is more than large enough to contain the object.  You have several
  1017. locks with keys.  The box has a locking ring which is more than large enough
  1018. to have a lock attached.  But your friend does not have the key to any
  1019. lock that you have.  How do you do it?  Note that you cannot send a key
  1020. in an unlocked box, since it might be copied.
  1021.  
  1022.  
  1023. ==> logic/locks.and.boxes.s <==
  1024. Attach a lock to the ring.  Send it to her.  She attaches her own lock
  1025. and sends it back.  You remove your lock and send it back to her.  She
  1026. removes her lock.
  1027.  
  1028. ==> logic/min.max.p <==
  1029. In a rectangular array of people, which will be taller, the tallest of the
  1030. shortest people in each column, or the shortest of the tallest people in each row?
  1031.  
  1032. ==> logic/min.max.s <==
  1033. Let T denote shortest of tall
  1034. Let S denote tallest of short
  1035.  
  1036. -------------------------------
  1037. |                             |
  1038. |                             |
  1039. |                S            |
  1040. |                             |
  1041. |                             |
  1042. |    T           X            |
  1043. |                             |
  1044. |                             |
  1045. |                             |
  1046. |                             |
  1047. -------------------------------
  1048.  
  1049. So T >= X >= S.
  1050.  
  1051.  
  1052. ==> logic/mixing.p <==
  1053. Start with a half cup of tea and a half cup of coffee. Take one tablespoon
  1054. of the tea and mix it in with the coffee. Take one tablespoon of this mixture
  1055. and mix it back in with the tea. Which of the two cups contains more of its
  1056. original contents?
  1057.  
  1058. ==> logic/mixing.s <==
  1059. Mixing Liquids
  1060.  
  1061. The two cups end up with the same volume of liquid they started with. The same
  1062. amount of tea was moved to the coffee cup as coffee to the teacup. Therefore
  1063. each cup contains the same amount of its original contents.
  1064.  
  1065. ==> logic/monty.52.p <==
  1066. Monty and Waldo play a game with N closed boxes.  Monty hides a
  1067. dollar in one box; the others are empty.  Monty opens the empty boxes
  1068. one by one.  When there are only two boxes left Waldo opens either box;
  1069. he wins if it contains the dollar.  Prior to each of the N-2 box
  1070. openings Waldo chooses one box and locks it, preventing Monty from opening
  1071. it next.  That box is then unlocked and cannot be so locked twice in a row.
  1072.  
  1073. What are the optimal strategies for Monty and Waldo and what is the
  1074. fair price for Waldo to pay to play the game?
  1075.  
  1076. ==> logic/monty.52.s <==
  1077. The fair price for large N is $0.6321205588285576784; I will offer
  1078. a proof along with optimal strategies.
  1079.  
  1080. Denote the game as G_N().  After (N-M) rounds of play, the game will have
  1081. the same form as G_M().  Depending on the strategies each of the M boxes
  1082. will have a probability p_i of containing the dollar.  Let Waldo lock
  1083. the M'th box (renumbering the boxes if necessary).  Denote the game and
  1084. Waldo's expected winnings in the game by
  1085.         G_M(p_1, p_2, ..., p_M)
  1086. where
  1087.         p_1 + p_2 + ... + p_M = 1
  1088.  
  1089. When
  1090.         p_2 = p_3 = p_4 = ... = p_M
  1091. we adopt the abbreviation
  1092.         G_M(b) = G_M(1 + b - Mb, b, b, b, b, ..., b)
  1093. and note that since probabilities are never negative:
  1094.         1 + b - Mb >= 0, or
  1095.         0 <= b <= 1 / (M-1)
  1096.  
  1097. Various G_M(p_1, p_2, ..., p_M) have difficult solutions but we are asked
  1098. only to solve G_M(1/M) and it turns out we can accomplish this by
  1099. considering only the games
  1100.         G_M(b)  where    1/M  <=  b  <=  1/(M-1)        [1]
  1101. Games of this form will be said to satisfy constraint [1].
  1102.  
  1103. Induction is used for one of the theorems, so we'd better solve G_2 and G_3
  1104. for our basis.
  1105.         G_2(p_1, p_2) = max (p_1, p_2)
  1106.         G_3(p_1, p_2, p_3) = max (p_1 + p_2, p_3)
  1107. since after Monty opens box #1, box #2 will have probability (p_1 + p_2)
  1108. and vice versa.  When the probabilities satisfy constraint [1]: 
  1109.         G_2(b) = G_2(1-b, b) = b
  1110.         G_3(b) = G_3(1-2b, b, b) = 1 - b
  1111.  
  1112. The proof of Theorem 1 is based on the probability p_i that box #i
  1113. contains the dollar.  (Of course this is Waldo's perceived probability:
  1114. Monty's probability would be 0 or 1.)  It may seem wrong for Waldo to
  1115. "forget" the game history and remember only the computed p_i.  For
  1116. example he may have previously locked some but not all of the boxes
  1117. and this fact is ignored except in the calculation of p_i.  Or Monty may
  1118. have some higher level "plan" which mightn't seem to translate directly
  1119. into probabilities.  But probability algebra obeys some simplifying
  1120. linearity rules and, if Monty keeps a "poker face", the probability model
  1121. is the only thing Waldo has to act on.
  1122.  
  1123. Especially paradoxical is the derivation of Waldo's p_i in his trivial
  1124. strategy below: he can adopt inferior but "correct" p_i to simplify the
  1125. analysis.
  1126.  
  1127. Theorem 1)
  1128.     If   b >= 1/M   then
  1129.     G_M(b) = G_[M-1]( (1-b) / (M-2) )
  1130.  
  1131. Proof)
  1132.     We will show that Monty and Waldo each have a strategy in G_M(b) to
  1133.     reduce the game to G_[M-1](b, q, q, ..., q) where q = (1-b) / (M-2)
  1134.     and where the boxes have been renumbered so that box #1 was box #M
  1135.     (the one Waldo locked) from the prior round and the new box #(M-1)
  1136.     is the one Waldo locks next.  Note that if Monty indeed arranges
  1137.     the probability mixture G_[M-1](b, q, q, q, q, ..., q) it won't
  1138.     matter which box Waldo locks (Box #1 has the only non-equal
  1139.     probability but Waldo cannot lock the same box twice in a row);
  1140.     this is a typical property of "saddlepoint" strategy.
  1141.  
  1142.     We will derive the necessary and sufficient condition for Monty to
  1143.     reduce any game G_M(p_1, p_2, p_3, ..., p_M) to a single game with
  1144.     the form G_[M-1](b, q, q, ..., q).  Using the numbering of G_M()
  1145.     let R(i,j) be the joint probability that Box #i contains the dollar
  1146.     and Box #j is opened by Monty in G_M().  We need consider only
  1147.         M >= 3
  1148.     Clearly,
  1149.         R(i, j) >= 0
  1150.         R(i, i) = 0
  1151.         R(i, M) = 0, i < M
  1152.         sum_over_j R(i,j) = p_i
  1153.     and to achieve q_2 = q_3 = ... = q_[M-1] in G_[M-1],
  1154.         R(1, j) = R(k, j)
  1155.             for 1 < j,k < M and j != k
  1156.         R(2, 1) = R(k, 1)
  1157.             for 2 < k < M
  1158.     and to make G_[M-1] be independent of Monty's play
  1159.         R(M, j)/R(1, j) = R(M, 2)/R(1, 2)
  1160.             for 2 < j < M
  1161.         R(M, 2)/R(1, 2) = R(M, 1)/R(2, 1)
  1162.  
  1163.     The above have a simple unique solution:
  1164.         R(i, j) = (1 - p_M) / (M - 2)  - p_j        [2]
  1165.             for i,j < M and i != j
  1166.         R(M, j) = p_M - p_j * p_M / (1 - p_M)        [3]
  1167.             for j < M
  1168.         p_j * (M-2) + p_M <= 1                [4]
  1169.  
  1170.     For the theorem we are given that G_M(b) satisfies constraint [1]
  1171.         1 / M  <=  b  <=  1 / (M - 1)
  1172.     which implies the weaker inequality
  1173.         (M - 3) / (M^2 - 3M + 1)  <=  b  <=  1 / (M - 1)
  1174.     and since for the constraint-[1] compliant G_M()
  1175.         p_j = b   or   p_j = (1+b-Mb)   for all j
  1176.     the inequality [4] follows directly.
  1177.  
  1178.     Hence Monty can transpose G_M(b) into G_[M-1]( (1-b) / (M-2) )
  1179.     whenever b >= 1/M and M >= 3.  (This G_[M-1] also happens to
  1180.     satisfy constraint [1] as needed for the next theorem.)
  1181.  
  1182.     It should be easy to argue that this strategy is optimal for Monty,
  1183.     but we want to derive Waldo's best strategy anyway and if it
  1184.     guarantees the same value we know we're at the "saddlepoint".
  1185.     If Waldo knows Monty has a non-optimal strategy he can take
  1186.     advantage of it, but we will just derive a strategy good enough
  1187.     to achieve the saddle-point value.
  1188.  
  1189.     Monty must transform G_M(b) into some
  1190.         G_[M-1](b, q_2, q_3, ..., q_[M-1])
  1191.     where Waldo has the choice of locking any of boxes #2 through #(M-1).
  1192.     If Waldo locks each of the (M-2) available boxes with probability
  1193.     1/(M-2) it is easily seen that the average probability that he
  1194.     locks the box with the dollar is (1-b) / (M-2) and the probabilities
  1195.     q_2, q_3, ..., q_[M-2] will also have the average value (1-b)/(M-2).
  1196.     If Waldo pretends to "forget" which box he picked before, he can
  1197.     take q_2 = q_3 = ... = (1-b)/(M-2) thereby constructing the same
  1198.     game Monty constructed with his saddlepoint strategy!
  1199.  
  1200.     In the above Waldo in effect "degraded" the accuracy of his
  1201.     probability estimates with the substitutions
  1202.         q_2' = (q_2 + q_3 + ... + q_[M-1]) / (M - 2)
  1203.         q_3' = (q_2 + q_3 + ... + q_[M-1]) / (M - 2)
  1204.           et cetera
  1205.     If Waldo "knows" more than this, he can pretend he doesn't!
  1206.     For example he can ask Monty to secretly shuffle the boxes.
  1207.  
  1208.     Thus either player can reduce
  1209.         G_M(b), b >= 1/M
  1210.     to
  1211.         G_[M-1]((1-b)/(M-2))
  1212.     so this must be the saddlepoint.
  1213.     Q.E.D.
  1214.  
  1215. Theorem 2)
  1216.     If   b >= 1/M   then
  1217.     G_M(b) = 1 - 1/2! + 1/3! - ... - (1-b)(-1)^M/(M-2)!
  1218.         = - sum (-1)^i/i! - (1-b)(-1)^M/(M-2)!
  1219.     where the sum is over i = 1, 2, 3, ..., M-3
  1220.  
  1221. Proof)
  1222.     The proof is by induction.  We know the theorem holds for M = 3
  1223.     and we will assume it holds for (M-1).  Set
  1224.         c = (1-b) / (M-2)
  1225.     We noted earlier that b <= 1/(M-1): otherwise p_1 = (1 + b - Mb)
  1226.     is negative; hence we obtain
  1227.         c = (1-b)/(M-2) >= (1 - 1/(M-1)) / (M-2)
  1228.     or simply
  1229.         c >= 1/(M-1)
  1230.     Thus the condition of the inductive hypothesis is satisfied and
  1231.         G_[M-1](c) = 1 - 1/2! + 1/3! - ... + (1-c)(-1)^M/(M-3)!
  1232.     But from Theorem 1
  1233.         G_M(b) = G_[M-1](c)
  1234.     and from the definition of c,
  1235.         c/(M-3)! = (1-b)/(M-2)!
  1236.     which establishes the theorem.
  1237.  
  1238. Theorem 3)
  1239.     G_M(1/M) = G_M(1/M, ..., 1/M) = 1 - 1/2! + 1/3! - ... -(-1)^M/M!
  1240.  
  1241. Proof)
  1242.     This follows directly from Theorem 2 and the observation that
  1243.         (1/M)/(M-2)! = 1/(M-1)! - 1/M!
  1244.  
  1245. For large M, G_M(1/M) approaches (1 - 1/e).  It will be a little bigger
  1246. when M is odd and a little smaller when M is even.  I've appended the
  1247. numeric values below.
  1248.  
  1249. % dc
  1250. [[Solution for M =]Plb1+pdsb]sy
  1251. 65k1sa1sblyx2sc[la1lc/-dsaplclyx*scla1lc/+dsaplclyx*sclzx]dszx
  1252. Solution for M =2
  1253. 0.50000000000000000000000000000000000000000000000000000000000000000
  1254. Solution for M =3
  1255. 0.66666666666666666666666666666666666666666666666666666666666666666
  1256. Solution for M =4
  1257. 0.62500000000000000000000000000000000000000000000000000000000000000
  1258. Solution for M =5
  1259. 0.63333333333333333333333333333333333333333333333333333333333333333
  1260. Solution for M =6
  1261. 0.63194444444444444444444444444444444444444444444444444444444444445
  1262. Solution for M =7
  1263. 0.63214285714285714285714285714285714285714285714285714285714285714
  1264. Solution for M =8
  1265. 0.63211805555555555555555555555555555555555555555555555555555555556
  1266. Solution for M =9
  1267. 0.63212081128747795414462081128747795414462081128747795414462081129
  1268. Solution for M =10
  1269. 0.63212053571428571428571428571428571428571428571428571428571428572
  1270.     . . .
  1271. Solution for M =52
  1272. 0.63212055882855767840447622983853913255418886896823216549216319831
  1273.  
  1274. P. S. There are related unsolved problems:
  1275. (a) what about G_M(p_1, p_2, ..., p_M) that do not fit the pattern used
  1276. in the above solution?
  1277. (b) what if two boxes contain dollars?  (first, what should the rules be?)
  1278.  
  1279. -- james@crc.ricoh.com (James Allen)
  1280.  
  1281. ==> logic/number.p <==
  1282. Mr. S. and Mr. P. are both perfect logicians, being able to correctly deduce
  1283. any truth from any set of axioms.  Two integers (not necessarily unique) are
  1284. somehow chosen such that each is within some specified range.  Mr. S.
  1285. is given the sum of these two integers; Mr. P. is given the product of these
  1286. two integers.  After receiving these numbers, the two logicians do not
  1287. have any communication at all except the following dialogue:
  1288. <<1>>   Mr. P.:  I do not know the two numbers.
  1289. <<2>>   Mr. S.:  I knew that you didn't know the two numbers.
  1290. <<3>>   Mr. P.:  Now I know the two numbers.
  1291. <<4>>   Mr. S.:  Now I know the two numbers.
  1292.  
  1293. Given that the above statements are absolutely truthful, what are the two
  1294. numbers?
  1295.  
  1296. ==> logic/number.s <==
  1297. The answer depends upon the ranges from which the numbers are chosen.
  1298.  
  1299. The unique solution for the ranges [2,62] through [2,500+] is:
  1300.  
  1301.   SUM   PRODUCT   X   Y
  1302.    17      52     4  13
  1303.  
  1304. The unique solution for the ranges [3,94] through [3,500+] is:
  1305.  
  1306.   SUM   PRODUCT   X   Y
  1307.    29     208    13  16
  1308.  
  1309. There are no unique solutions for the ranges starting with 1,
  1310. and there are no solutions for ranges starting with numbers above 3.
  1311.  
  1312. A program to compute the possible pairs is included below.
  1313.  
  1314. #include <stdio.h>
  1315.  
  1316. /*
  1317.  
  1318. BEGINNING OF PROBLEM STATEMENT:
  1319. Mr. S. and Mr. P. are both perfect logicians, being able to correctly deduce
  1320. any truth from any set of axioms.  Two integers (not necessarily unique) are
  1321. somehow chosen such that each is within some specified range.  Mr. S.
  1322. is given the sum of these two integers; Mr. P. is given the product of these
  1323. two integers.  After receiving these numbers, the two logicians do not
  1324. have any communication at all except the following dialogue:
  1325. <<1>>   Mr. P.:  I do not know the two numbers.
  1326. <<2>>   Mr. S.:  I knew that you didn't know the two numbers.
  1327. <<3>>   Mr. P.:  Now I know the two numbers.
  1328. <<4>>   Mr. S.:  Now I know the two numbers.
  1329.  
  1330. Given that the above statements are absolutely truthful, what are the two
  1331. numbers?
  1332.  
  1333. END OF PROBLEM STATEMENT
  1334.  
  1335.  */
  1336.  
  1337. #define SMALLEST_MIN    1
  1338. #define LARGEST_MIN    10
  1339. #define SMALLEST_MAX    50
  1340. #define LARGEST_MAX    500
  1341.  
  1342. long P[(LARGEST_MAX + 1) * (LARGEST_MAX + 1)];        /* products */
  1343. long S[(LARGEST_MAX + 1) + (LARGEST_MAX + 1)];        /*   sums   */
  1344.  
  1345. find(long min, long max)
  1346. {
  1347.     long i, j;
  1348.     /*
  1349.      *    count factorizations in P[]
  1350.      *    all P[n] > 1 satisfy <<1>>.
  1351.      */
  1352.     for(i = 0; i <= max * max; ++i)
  1353.         P[i] = 0;
  1354.  
  1355.     for(i = min; i <= max; ++i)
  1356.         for(j = i; j <= max; ++j)
  1357.             ++P[i * j];
  1358.  
  1359.     /*
  1360.      *    decompose possible SUMs and check factorizations
  1361.      *        all S[n] == min - 1 satisfy <<2>>.
  1362.      */
  1363.     for(i = min + min; i <= max + max; ++i) {
  1364.  
  1365.         for(j = i / 2; j >= min; --j)
  1366.             if(P[j * (i - j)] < 2)
  1367.                 break;
  1368.  
  1369.         S[i] = j;
  1370.     }
  1371.  
  1372.     /*
  1373.      *    decompose SUMs which satisfy <<2>> and see which products
  1374.      *    they produce.  All (P[n] / 1000 == 1) satisfy <<3>>.
  1375.      */
  1376.     for(i = min + min; i <= max + max; ++i)
  1377.         if(S[i] == min - 1)
  1378.             for(j = i / 2; j >= min; --j)
  1379.                 if(P[j * (i - j)] > 1)
  1380.                     P[j * (i - j)] += 1000;
  1381.     /*
  1382.      *    decompose SUMs which satisfy <<2>> again and see which products
  1383.      *    satisfy <<3>>.  Any (S[n] == 999 + min) satisfies <<4>>
  1384.      */
  1385.     for(i = min + min; i <= max + max; ++i)
  1386.         if(S[i] == min - 1)
  1387.             for(j = i / 2; j >= min; --j)
  1388.                 if(P[j * (i - j)] / 1000 == 1)
  1389.                     S[i] += 1000;
  1390.     /*
  1391.      *    find the answer(s) and print them
  1392.      */
  1393.     printf("[%d,%d]\n",min,max);
  1394.     for(i = min + min; i <= max + max; ++i)
  1395.         if(S[i] == 999 + min)
  1396.             for(j = i / 2; j >= min; --j)
  1397.                 if(P[j * (i - j)] / 1000 == 1)
  1398.                     printf("{ %d %d }: S = %d, P = %d\n",
  1399.                         i - j, j, i, (i - j)  * j);
  1400. }
  1401.  
  1402. main()
  1403. {
  1404.     long min, max;
  1405.  
  1406.     for (min = SMALLEST_MIN; min <= LARGEST_MIN; min ++)
  1407.         for (max = SMALLEST_MAX; max <= LARGEST_MAX; max++)
  1408.         find(min,max);
  1409. }
  1410.  
  1411. -------------------------------------------------------------------------
  1412. =            Jeff Kenton    (617) 894-4508            =
  1413. =                jkenton@world.std.com            =
  1414. -------------------------------------------------------------------------
  1415.